home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- * QTKSAMPL.C version 1.0
- * Copyright Apple Computer, Inc. 1993 -1995, All Rights Reserved.
- *******************************************************************************/
-
- /****************************************************************************
- PROGRAM: QTKSampl.c
- VERSION: 1.0
-
- PURPOSE: QuickTake sample application.
-
- FUNCTIONS:
-
- WinMain() - Calls initialization function, processes message loop
- InitApplication() - Initializes window data and registers window
- InitInstance() - Saves instance handle and creates main window
- MainWndProc() - Processes messages
-
- CAMERA FUNCTIONS:
-
- CameraInitialize() - Connects to camera and load infomation
- CameraTerminet() - Terminates camera connection
- CameraUnloadThumbnail() - Unloads memory allocated for thumbnail
- CameraLoadThumbnail() - Allocates memory for thumbnail and load
- thumbnail data from camera
- CameraPaintThumbnail() - Paint thumbnail
- CameraUnloadPicture() - Unloads memory allocated for picture
- CameraLoadPicture() - Allocates memory for picture and load
- picture data from camera
- CameraPaintPicture() - Paint picture
- CameraError() - Camera error message box.
-
- ****************************************************************************/
-
- #include "qtksampl.h" /* specific to this program */
-
- HANDLE hInst; // This instance
- HWND hMainWnd; // Handle to main Window
- BOOL bCameraInitialized; // Whether camera is initialized?
- QTKCameraInfo qtkInfo = {0}; // CameraInfo
- CmPictureInfo pictInfo; // PictureInfo of last picture
- char cmThumb[2400]; // Data used to store thumbnail data
- char ipm[kIPMContextSize];
- HANDLE hThumbnail; // Handle to thumbnail data
- HANDLE hPicture; // Handle to picture data
-
-
-
- /****************************************************************************
-
- FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
-
- PURPOSE: calls initialization function, processes message loop
-
- ****************************************************************************/
-
- int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
- HANDLE hInstance; /* current instance */
- HANDLE hPrevInstance; /* previous instance */
- LPSTR lpCmdLine; /* command line */
- int nCmdShow; /* show-window type (open/icon) */
- {
- MSG msg; /* message */
-
- if (hPrevInstance) /* Only one instance of app allowed */
- return (FALSE);
-
- /* Perform initializations of application */
-
- if (!InitApplication(hInstance))
- return (FALSE);
-
- /* Perform initializations of this instance */
-
- if (!InitInstance(hInstance, nCmdShow))
- return (FALSE);
-
- /* Acquire and dispatch messages until a WM_QUIT message is received. */
-
- while (GetMessage(
- &msg, // Message structure
- NULL, // Handle of window receiving the message
- NULL, // Lowest message to examine
- NULL)) // Highest message to examine
- {
- TranslateMessage(&msg); // Translates virtual key codes
- DispatchMessage(&msg); // Dispatches message to window
- }
- return (msg.wParam); // Returns the value from PostQuitMessage
- }
-
-
- /****************************************************************************
-
- FUNCTION: InitApplication(HANDLE)
-
- PURPOSE: Initializes window data and registers window class
-
- ****************************************************************************/
-
- BOOL InitApplication(hInstance)
- HANDLE hInstance; /* current instance */
- {
- WNDCLASS wc;
-
- /* Fill in window class structure with parameters that describe the */
- /* main window. */
-
- wc.style = NULL; /* Class style(s). */
- wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */
- /* windows of this class. */
- wc.cbClsExtra = 0; /* No per-class extra data. */
- wc.cbWndExtra = 0; /* No per-window extra data. */
- wc.hInstance = hInstance; /* Application that owns the class. */
- wc.hIcon = NULL;
- wc.hCursor = NULL;
- wc.hbrBackground = GetStockObject(WHITE_BRUSH);
- wc.lpszMenuName = "QuickTakeSampleMenu"; /* Name of menu resource in .RC file. */
- wc.lpszClassName = "QuickTakeSampleClass"; /* Name used in call to CreateWindow. */
-
- /* Register the window class and return success/failure code. */
-
- return (RegisterClass(&wc));
- }
-
-
- /****************************************************************************
-
- FUNCTION: InitInstance(HANDLE, int)
-
- PURPOSE: Saves instance handle and creates main window
-
- ****************************************************************************/
-
- BOOL InitInstance(hInstance, nCmdShow)
- HANDLE hInstance; /* Current instance identifier. */
- int nCmdShow; /* Param for first ShowWindow() call. */
- {
- /* Save the instance handle in static variable, which will be used in */
- /* many subsequence calls from this application to Windows. */
-
- hInst = hInstance;
-
- /* Create a main window for this application instance. */
-
- hMainWnd = CreateWindow(
- "QuickTakeSampleClass", // See RegisterClass() call.
- "QuickTake Sample Application", // Text for window title bar.
- WS_OVERLAPPEDWINDOW, // Window style.
- CW_USEDEFAULT, // Default horizontal position.
- CW_USEDEFAULT, // Default vertical position.
- CW_USEDEFAULT, // Default width.
- CW_USEDEFAULT, // Default height.
- NULL, // Overlapped windows have no parent.
- NULL, // Use the window class menu.
- hInstance, // This instance owns this window.
- NULL // Pointer not needed.
- );
-
- /* If window could not be created, return "failure" */
-
- if (!hMainWnd)
- return (FALSE);
-
- /* Make the window visible; update its client area; and return "success" */
-
- ShowWindow(hMainWnd, nCmdShow); // Show the window
- UpdateWindow(hMainWnd); // Sends WM_PAINT message
- return (TRUE);
-
- }
-
- /****************************************************************************
-
- FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
-
- PURPOSE: Processes messages
-
- ****************************************************************************/
-
- long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
- HWND hWnd; /* window handle */
- UINT message; /* type of message */
- WPARAM wParam; /* additional information */
- LPARAM lParam; /* additional information */
- {
- switch (message)
- {
- case WM_CREATE:
- bCameraInitialized = FALSE;
- hThumbnail = 0;
- hPicture = 0;
- break;
-
- case WM_INITMENU:
- break;
-
- case WM_COMMAND: /* message: command from application menu */
-
- switch(wParam)
- {
- case IDM_OPEN:
-
- if (bCameraInitialized)
- break;
- if (CameraInitialize(hInst) == 0)
- {
- bCameraInitialized = TRUE;
- hThumbnail = CameraLoadThumbnail();
- hPicture = CameraLoadPicture();
- InvalidateRect(hWnd, NULL, TRUE);
- }
- else
- CameraTerminate();
- break;
- }
- break;
-
- case WM_PAINT:
-
- {
- PAINTSTRUCT ps;
-
- BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);
- CameraPaintThumbnail(ps.hdc, hThumbnail, 0, 0);
- CameraPaintPicture(
- ps.hdc,
- hPicture,
- qtkInfo.cameraInfo.thumbnailImageWidth,
- 0);
- EndPaint (hWnd, (LPPAINTSTRUCT) &ps);
- return (TRUE);
- break;
- }
-
- case WM_DESTROY: /* message: window being destroyed */
-
- if (bCameraInitialized)
- {
- CameraUnloadPicture(hPicture);
- CameraUnloadThumbnail(hThumbnail);
- CameraTerminate();
- }
- PostQuitMessage(0);
- break;
-
- default: /* Passes it on if unproccessed */
- return (DefWindowProc(hWnd, message, wParam, lParam));
- }
- return (NULL);
- }
-
-
- /****************************************************************************
-
- FUNCTION: CameraInitialize(HANDLE)
-
- PURPOSE: Open driver, Connect camera to port, Get cameraInfo,
- GetPictureInfo
-
- RETURN: 0, if no error
- otherwise, error code return from QuickTake driver or ipm
-
- ****************************************************************************/
-
- OSErr CameraInitialize(HANDLE hInst)
- {
- OSErr err;
- int currPort;
- int commPort = 0; // 0 - Test all ports (1-4)
- int nPicturesTaken;
-
- // Open QuickTake Driver
- qtkInfo.cameraData = hInst;
- err = CmOpenDriver(&qtkInfo.cameraData);
- if (err)
- {
- qtkInfo.cameraData = 0;
- return CameraError("OpenDriver", err);
- }
-
- // Connect camera to port
- for (currPort = 1; currPort <= 4; currPort++)
- {
- CmPortInfoHandlePtr portInfoPtr; // handle for modem port
-
- portInfoPtr = &qtkInfo.portHandle;
-
- // Check to see if this is a port we should check
- if ((commPort == 0) || (currPort == commPort))
- {
- // Get the port info...
- err = CmGetPortInfo(qtkInfo.cameraData, currPort, portInfoPtr);
- if (err == 0)
- {
- // Is the port availalble?
- if ((**portInfoPtr)->portStatus & 0x00000001L)
- {
- // Port available, try to connect
- err = CmConnect(qtkInfo.cameraData, *portInfoPtr);
- if (err == 0)
- {
- qtkInfo.connectedPort = currPort;
- break;
- }
- else
- {
- CmDisposePortInfo(qtkInfo.cameraData, *portInfoPtr);
- *portInfoPtr = NULL;
- }
- }
- }
- }
- }
- if (qtkInfo.connectedPort == 0)
- return CameraError("ConnectCamera", err);
-
- // Check camera ready?
- err = CmCheckIfReady(qtkInfo.cameraData);
- if (err)
- return CameraError("CameraReady", err);
-
- // Get camera info
- err = CmGetCameraInfo(qtkInfo.cameraData, &qtkInfo.cameraInfo);
- if (err)
- return CameraError("CameraGetInfo", err);
-
- // Check whether there is at least one picture in the camera
- nPicturesTaken = qtkInfo.cameraInfo.noPicturesTaken;
- if (nPicturesTaken == 0)
- return CameraError("CameraEmpty", 0);
-
- // Get last picture info
- err = CmGetPictureInfo(qtkInfo.cameraData, nPicturesTaken, &pictInfo);
- if (err)
- return CameraError("CameraGetPictureInfo", err);
-
- return err;
- }
-
-
- /****************************************************************************
-
- FUNCTION: CameraTerminate(void)
-
- PURPOSE: Disconnect camera, Close driver
-
- RETURN: 0
-
- ****************************************************************************/
-
- OSErr CameraTerminate(void)
- {
- // Disconnect
- if (qtkInfo.cameraData)
- {
- if (qtkInfo.connectedPort > 0)
- {
- // Disconnect camera
- CmDisconnect(qtkInfo.cameraData);
- qtkInfo.connectedPort = 0;
-
- // Dispose PortInfo
- CmDisposePortInfo(qtkInfo.cameraData, qtkInfo.portHandle);
- qtkInfo.portHandle = 0;
- }
- }
-
- // Close driver
- if (qtkInfo.cameraData)
- {
- CmCloseDriver(qtkInfo.cameraData);
- qtkInfo.cameraData = 0;
- }
- return 0;
- }
-
-
- /****************************************************************************
-
- FUNCTION: CameraUnloadThumbnail(HANDLE)
-
- PURPOSE: Free up memory allocated for thumbnail.
-
-
- ****************************************************************************/
-
- void CameraUnloadThumbnail(HANDLE hThumbnail)
- {
- GlobalFree(hThumbnail);
- }
-
- /****************************************************************************
-
- FUNCTION: CameraLoadThumbnail(void)
-
- PURPOSE: Allocate memory for the thumbnail and load the thumbnail
- of the last picture in the camera into memory.
-
- RETURN: 0, if error
- otherwise, memory handle to thumbnail data.
-
- ****************************************************************************/
-
- HANDLE CameraLoadThumbnail(void)
- {
- OSErr err;
- long bytesRead;
- DWORD szThumbnail;
- HANDLE hThumbnail;
- BufferPtr pThumbnail;
-
- // Load compressed thumbnail data
- err = CmGetThumbnailImage(
- qtkInfo.cameraData,
- pictInfo.pictureNo,
- cmThumb,
- 2400,
- TRUE,
- &bytesRead);
- if (err)
- {
- CameraError("LoadThumbnail", err);
- return 0;
- }
-
- // Allocate memory for thumbnail image
- szThumbnail = qtkInfo.cameraInfo.thumbnailImageWidth * 3 *
- qtkInfo.cameraInfo.thumbnailImageHeight;
- hThumbnail = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, szThumbnail);
- if (hThumbnail == 0)
- {
- CameraError("LoadThumbnail", kCmNoMemoryError);
- return 0;
- }
- pThumbnail = GlobalLock(hThumbnail);
- if (pThumbnail == 0)
- {
- GlobalFree(hThumbnail);
- CameraError("LoadThumbnail", kCmNoMemoryError);
- return 0;
- }
-
- // Convert from compressed thumbnail to thumbnail image
- err = IpInitIPM(ipm);
- if (err == 0)
- {
- // Calculate color table
- err = IpCalculateColorTables(ipm);
- if (err == 0)
- {
- ImageAttribute attr;
-
- // Prepare image attributes
- attr.aImgWidth = qtkInfo.cameraInfo.thumbnailImageWidth;
- attr.aImgLength = qtkInfo.cameraInfo.thumbnailImageHeight;
- attr.aPixelType = kDIB24;
- attr.aAngle = kImgRot0;
- attr.aStartingLine = 0;
- attr.aLinesPerBand = 0;
- attr.firstBand = TRUE;
- attr.lastBand = TRUE;
- attr.aCompressionType = qtkInfo.cameraInfo.thumbImageDatCompMode;
-
- // Convert
- err = IpConvertThumbnailToRGB(
- ipm,
- cmThumb,
- pThumbnail,
- &attr);
- }
- IpEndIPM(ipm);
- }
- if (err)
- {
- CameraError("LoadThumbnail", err);
- GlobalUnlock(hThumbnail);
- GlobalFree(hThumbnail);
- return 0;
- }
- else
- {
- GlobalUnlock(hThumbnail);
- return hThumbnail;
- }
- }
-
- /****************************************************************************
-
- FUNCTION: CameraPaintThumbnail(HDC, HANDLE, int, int)
-
- PURPOSE: Paint the thumbnail as bitmap.
-
- ****************************************************************************/
-
- void CameraPaintThumbnail(
- HDC hDC, // Handle to display context
- HANDLE hThumbnail, // Handle to thumbnail memory
- int x, // Horizontal offset
- int y) // Vertical offset
- {
- BITMAPINFO bmpInfo; // BitmapInfo for display
- LPSTR pThumbnail; // Thumbnail memory pointer
-
- if (!hThumbnail)
- return;
- bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmpInfo.bmiHeader.biWidth = qtkInfo.cameraInfo.thumbnailImageWidth;
- bmpInfo.bmiHeader.biHeight = qtkInfo.cameraInfo.thumbnailImageHeight;
- bmpInfo.bmiHeader.biPlanes = 1;
- bmpInfo.bmiHeader.biBitCount = 24;
- bmpInfo.bmiHeader.biCompression = BI_RGB;
- bmpInfo.bmiHeader.biSizeImage = bmpInfo.bmiHeader.biHeight *
- bmpInfo.bmiHeader.biWidth * 3;
- bmpInfo.bmiHeader.biClrUsed = 0;
- bmpInfo.bmiHeader.biClrImportant = 0;
- bmpInfo.bmiHeader.biXPelsPerMeter = 0;
- bmpInfo.bmiHeader.biYPelsPerMeter = 0;
-
- pThumbnail = GlobalLock(hThumbnail);
-
-
- SetDIBitsToDevice(hDC, x, y,
- (WORD)bmpInfo.bmiHeader.biWidth,
- (WORD)bmpInfo.bmiHeader.biHeight,
- 0, 0, 0, (WORD)bmpInfo.bmiHeader.biHeight,
- pThumbnail, &bmpInfo, DIB_RGB_COLORS);
- GlobalUnlock(hThumbnail);
- }
-
- /****************************************************************************
-
- FUNCTION: CameraUnloadPicture(HANDLE)
-
- PURPOSE: Free up memory allocated for picture image.
-
-
- ****************************************************************************/
-
- void CameraUnloadPicture(HANDLE hPicture)
- {
- GlobalFree(hPicture);
- }
-
-
- /****************************************************************************
-
- FUNCTION: IPMProgressCallback(short, short, long)
-
- PURPOSE: Callback function for IPM do update progress.
-
- RETURN: 0
-
- ****************************************************************************/
-
- static OSErr far pascal IPMProgressCallback
- (
- short msg,
- short percent,
- long refcon
- )
- {
- if (msg == 1)
- percent = percent; // Do nothing in this app
- return 0;
- }
-
- /****************************************************************************
-
- FUNCTION: CameraLoadPicture(void)
-
- PURPOSE: Allocate memory for the picture and load the
- last picture in the camera into memory.
-
- RETURN: 0, if error
- otherwise, memory handle to picture data.
-
- ****************************************************************************/
-
- HANDLE CameraLoadPicture(void)
- {
- HANDLE hCmImage;
- BufferPtr pCmImage;
- DWORD szCmImage;
- HANDLE hImage;
- unsigned char huge *pImage;
- DWORD szImage;
- long bytesRead;
- OSErr err;
-
- // Allocate compressed image memory
- szCmImage = pictInfo.imageDataSize;
- hCmImage = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, szCmImage);
- if (hCmImage == 0)
- {
- CameraError("LoadThumbnail", kCmNoMemoryError);
- return 0;
- }
- pCmImage = (BufferPtr)GlobalLock(hCmImage);
- if (pCmImage == 0)
- {
- GlobalFree(hCmImage);
- CameraError("LoadPicture", kCmNoMemoryError);
- return 0;
- }
-
- // Get compressed image from camera
- err = CmGetFullSizeImage(
- qtkInfo.cameraData,
- pictInfo.pictureNo,
- pCmImage,
- szCmImage,
- TRUE,
- &bytesRead);
- if (err)
- {
- GlobalUnlock(hCmImage);
- GlobalFree(hCmImage);
- CameraError("LoadPicture", kCmNoMemoryError);
- return 0;
- }
-
- // Allocate memory for picture image
- szImage = (DWORD)pictInfo.width * 3L * (DWORD)pictInfo.height;
- hImage = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, szImage);
- if (hImage == 0)
- {
- GlobalUnlock(hCmImage);
- GlobalFree(hCmImage);
- CameraError("LoadPicture", kCmNoMemoryError);
- return 0;
- }
- pImage = GlobalLock(hImage);
- if (pImage == 0)
- {
- GlobalUnlock(hCmImage);
- GlobalFree(hCmImage);
- GlobalFree(hImage);
- CameraError("LoadPicture", kCmNoMemoryError);
- return 0;
- }
-
- // Convert from compressed picture to picture image
- err = IpInitIPM(ipm);
- if (err == 0)
- {
- err = IpCalculateColorTables(ipm);
- if (err == 0)
- {
- ImageAttribute attr;
- ProgressCallBack ipmCB;
-
- // Prepare image attributes
- attr.aImgWidth = pictInfo.width;
- attr.aImgLength = pictInfo.height;
- attr.aPixelType = kDIB24;
- attr.aAngle = kImgRot0;
- attr.aStartingLine = 0;
- attr.aLinesPerBand = pictInfo.height;
- attr.firstBand = TRUE;
- attr.lastBand = TRUE;
- attr.aCompressionType = pictInfo.dataCompressionMode;
-
- // Setup callback function
- ipmCB.callBackProc = (IpProgressProcPtr)IPMProgressCallback;
- ipmCB.refcon = 0;
-
- err = IpConvertImageToRGB(
- ipm,
- pCmImage,
- (unsigned char huge *)pImage,
- &attr,
- &ipmCB);
- }
- IpEndIPM(ipm);
- }
-
- // Free up compressed picture memory
- GlobalUnlock(hCmImage);
- GlobalFree(hCmImage);
-
- if (err)
- {
- // Report error
- CameraError("LoadPicture", err);
-
- // Free up picture memory
- GlobalUnlock(hImage);
- GlobalFree(hImage);
- return 0;
- }
- else
- {
- GlobalUnlock(hImage);
- return hImage;
- }
- }
-
-
- /****************************************************************************
-
- FUNCTION: CameraPaintPicture(HDC, HANDLE, int, int)
-
- PURPOSE: Paint the picture.
-
- ****************************************************************************/
- void CameraPaintPicture(HDC hDC, HANDLE hPicture, int x, int y)
- {
- BITMAPINFO bmpInfo;
- unsigned char huge *pPicture;
-
- if (!hPicture)
- return;
- bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmpInfo.bmiHeader.biWidth = pictInfo.width;
- bmpInfo.bmiHeader.biHeight = pictInfo.height;
- bmpInfo.bmiHeader.biPlanes = 1;
- bmpInfo.bmiHeader.biBitCount = 24;
- bmpInfo.bmiHeader.biCompression = BI_RGB;
- bmpInfo.bmiHeader.biSizeImage = bmpInfo.bmiHeader.biHeight *
- bmpInfo.bmiHeader.biWidth * 3;
- bmpInfo.bmiHeader.biClrUsed = 0;
- bmpInfo.bmiHeader.biClrImportant = 0;
- bmpInfo.bmiHeader.biXPelsPerMeter = 0;
- bmpInfo.bmiHeader.biYPelsPerMeter = 0;
-
- pPicture = GlobalLock(hPicture);
- SetDIBitsToDevice(hDC, x, y,
- (WORD)bmpInfo.bmiHeader.biWidth,
- (WORD)bmpInfo.bmiHeader.biHeight,
- 0, 0, 0, (WORD)bmpInfo.bmiHeader.biHeight,
- pPicture, &bmpInfo, DIB_RGB_COLORS);
- GlobalUnlock(hPicture);
- }
-
- /****************************************************************************
-
- FUNCTION: CameraError(LPSTR, OSErr)
-
- PURPOSE: Output camera error and return err code.
-
- ****************************************************************************/
-
- OSErr CameraError(LPSTR cmdString, OSErr err)
- {
- char errString[256];
-
- if (err)
- {
- wsprintf(errString, "%s (%d)", cmdString, err);
- MessageBox(hMainWnd, errString, "Error", MB_ICONEXCLAMATION | MB_OK);
- }
- return err;
- }
-